import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-deploy',
	templateUrl: './deploy.component.html',
})
export class DeployComponent implements OnInit {
	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			antennaName:{
				validators: {
					notEmpty: {
						message: '请输入天线名称'
					}
				}
			},
			type:{
				validators: {
					notEmpty: {
						message: '请选择天线类型'
					}
				}
			}
		}
	}
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		searchKey: ''
	}
	formType: any=''
	appletsFrom:any={
		id: '',
		antennaName: '',
		type: '',
		combinationAntenna: 0
	}
	isCheck: boolean= true
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.load();
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:common.stringTrim(this.tableParam.searchKey)
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("gpsAntenna/list.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	// 添加
	table_add(modal){
		this.formType = 'new';
		this.appletsFrom.id = '';
		this.appletsFrom.antennaName = '';
		this.appletsFrom.type = '';
		this.appletsFrom.combinationAntenna = 0;
		this.isCheck = true;
		$("#appletsFrom").data('bootstrapValidator').resetForm();
		modal.show();
	}
	// 关闭弹窗
	appletsClose(modal){
		modal.hide();
		this.appletsFrom.id = '';
		this.appletsFrom.antennaName = '';
		this.appletsFrom.type = '';
		this.appletsFrom.combinationAntenna = 0;
		this.isCheck = true;
	}
	// 提交弹窗
	appletsSubmit(modal){
		$('#appletsFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#appletsFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			if(this.formType == 'updata'){
				delete this.appletsFrom.combinationAntenna
			}
			this.jsonApiService.http_obj("gpsAntenna/updateGpsAntennaInfo.html",this.appletsFrom).subscribe(data=> {
				if (data.success) {
					if(this.formType == 'updata'){
						this.jsonApiService.message('编辑成功');
					}else if(this.formType == 'new'){
						this.jsonApiService.message('添加成功');
					}
					this.load();
					this.appletsClose(modal);
					this.getGpsInfo() //全局请求 天线配置接口 GPS天线/GSM天线
				}else{
					this.jsonApiService.message(data.message);
				}
			})
		}
	}
	// 编辑弹窗
	table_edit(row,modal){
		this.formType = 'updata';
		$("#appletsFrom").data('bootstrapValidator').resetForm();
		this.appletsFrom.id = row.id;
		this.appletsFrom.antennaName = row.antennaName;
		this.appletsFrom.type = row.type;
		this.appletsFrom.combinationAntenna = row.combinationAntenna;
		modal.show();
	}
	// 删除数据
	table_del(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'ids=' + row.id;
			this.jsonApiService.http("gpsAntenna/batchDeleted.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
					this.getGpsInfo() //全局请求 天线配置接口 GPS天线/GSM天线
				}
			})
		})
	}
	GPSChange(value){
		this.appletsFrom.type = value
	}
	//全局请求 天线配置接口 GPS天线/GSM天线
	getGpsInfo(){
		this.jsonApiService.http("gpsAntenna/getGpsInfo.html", '').subscribe(data => {
			if (data.success) {
				var obj;
				allSelectOptionsObj.gpsOptions = [];
				allSelectOptionsObj.gsmOptions = [];
				for(var i=0;i<data.data.length;i++){
					if(data.data[i].type == 'GPS天线'){
						obj = {
							value: data.data[i].antennaName,
							text: data.data[i].antennaName
						}
						allSelectOptionsObj.gpsOptions.push(obj);
					}else if(data.data[i].type == 'GSM天线'){
						obj = {
							value: data.data[i].antennaName,
							text: data.data[i].antennaName
						}
						allSelectOptionsObj.gsmOptions.push(obj);
					}
				}

				console.log(allSelectOptionsObj)
			}
		})
	}
	clickRecord(value){
		this.appletsFrom.combinationAntenna = value
		if(value == '1'){
			this.isCheck = false
		}else{
			this.isCheck = true
		}
	}
}